/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.data;

import com.tlf.dyjc5.service.RsjService;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dusin.web.util.CommonWebUtil;
import org.dusin.web.util.JsonUtil;
import org.dusin.web.util.TheUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author tengyu
 */
public class RsjLoad extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            RsjService rsjService = (RsjService) context.getBean("rsjService");

            String command = request.getParameter("cmd");
            List<Map<String, Object>> list =null;
            String currentOrgCode = (String) request.getSession().getAttribute("currentOrgCode");
            String lock = request.getParameter("lock");
            if ("general".equals(command)) {                
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo"); 

                String condition = TheUtil.getPointSelectCondition(request);                
                
                list = rsjService.findAll(currentOrgCode, dateFrom, dateTo, condition, start, limit,lock);
            }if ("recall".equals(command)) {                
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");               
                
                String condition = TheUtil.getPointSelectCondition(request);                
                list = rsjService.findAllDataRecall(currentOrgCode, dateFrom, dateTo, condition, start, limit);
            }if ("recallLock".equals(command)) {                
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");               
                
                String condition = TheUtil.getPointSelectCondition(request);                
                list = rsjService.findAllDataRecallLock(currentOrgCode, dateFrom, dateTo, condition, start, limit);
            }if ("lock".equals(command)) {    //锁定月数据时显示的数据                
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));                
                String date = request.getParameter("endDate");               
                
                String condition = TheUtil.getPointSelectCondition(request);                
                list = rsjService.findAllLock(currentOrgCode,  date, condition, start, limit);
            }else if("rangeData".equals(command)){  //某个监测点多日数据显示
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                long pointId = Integer.parseInt(request.getParameter("pointId"));
                list = rsjService.findAllRangeByPointId(pointId, dateFrom, dateTo, start, limit,lock);
                
            }else if("extreme".equals(command)){
                String dayFromStr = request.getParameter("dayFrom");
                String dayToStr = request.getParameter("dayTo");
                Timestamp dayFrom = Timestamp.valueOf(dayFromStr + " 00:00:00.0");
                Timestamp dayTo = Timestamp.valueOf(dayToStr + " 00:00:00.0");                
                long pointId = Integer.parseInt(request.getParameter("pointId"));                
                list = rsjService.findAllByExtreme(currentOrgCode,pointId,dayFrom,dayTo,"",0,10000);
            }else if("range".equals(command)){
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                           
                long pointId = Integer.parseInt(request.getParameter("pointId"));                
                list = rsjService.findAllRangeByPointId(pointId,dateFrom,dateTo,0,9999,lock);
            }else if("same".equals(command)){
                //String dayFromStr = request.getParameter("dayFrom");
                //String dayToStr = request.getParameter("dayTo");
                //System.out.println(dayFromStr+"-00 00:00:00.0");
                //Timestamp dayFrom = Timestamp.valueOf(dayFromStr + "-01 00:00:00.0");
                //Timestamp dayTo = Timestamp.valueOf(dayToStr + " 00:00:00.0");
                //int day = dayTo.getDate();
                //System.out.println(day);
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                long pointId = Integer.parseInt(request.getParameter("pointId"));  
                int day = Integer.parseInt(dateTo.substring(6,8));
                list = rsjService.findAllSameDayByPointId(pointId, dateFrom, dateTo, day);
            }else if("report".equals(command)){
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String beginTimeStr = request.getParameter("beginDate");
                String endTimeStr = request.getParameter("endDate");
                Timestamp beginTime = Timestamp.valueOf(beginTimeStr + " 00:00:00.0");
                Timestamp endTime = Timestamp.valueOf(endTimeStr + " 00:00:00.0");
                String condition = TheUtil.getPointSelectCondition(request);
                list = rsjService.findAllReport(currentOrgCode, beginTime, endTime, condition, start, limit);
            }else if("reportLock".equals(command)){
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");                
                String condition = TheUtil.getPointSelectCondition(request);
                list = rsjService.findAll(currentOrgCode, dateFrom, dateTo, condition, start, limit,lock);
            }else if("rsjOrg".equals(command)){  //单位日数剧
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String date = request.getParameter("dateTo");
                //list=rsjService.findAllOrgRsj(currentOrgCode,date,range,reportStatus,"");
                list=rsjService.findAllOrgRsjV2(currentOrgCode,date,range,reportStatus,"");
            }else if("rsjLockOrg".equals(command)){  //单位日数剧
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String date = request.getParameter("dateTo");
                //list=rsjService.findAllOrgRsj(currentOrgCode,date,range,reportStatus,"");
                list=rsjService.findAllOrgRsjLockV2(currentOrgCode,date,range,reportStatus,"");
            }else if("rsjSubOrg".equals(command)){  //下级单位日数剧
                String date = request.getParameter("dateTo");
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                list=rsjService.findAllSubOrgRsj(currentOrgCode,date,range,reportStatus,"");
            }else if("rsjLockSubOrg".equals(command)){  //单位日数剧
                String date = request.getParameter("dateTo");
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                list=rsjService.findAllSubOrgRsjLock(currentOrgCode,date,range,reportStatus,"");
            }else if("rsjRangeOrg".equals(command)){  //单位日数剧环比
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                list=rsjService.findAllOrgRsj(currentOrgCode,dateFrom,dateTo,range,reportStatus,"",start,limit);
            }else if("rsjLockRangeOrg".equals(command)){  //单位日数剧环比
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                list=rsjService.findAllOrgRsjLock(currentOrgCode,dateFrom,dateTo,range,reportStatus,"",start,limit);
            }else if("rsjRealOrg".equals(command)){  //日合格率当月走势
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                list=rsjService.findAllOrgRsjTheMonth(currentOrgCode,range,reportStatus,"");
            }else if("rsjLockRealOrg".equals(command)){  //日合格率当月走势
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                list=rsjService.findAllOrgRsjLockTheMonth(currentOrgCode,range,reportStatus,"");
            }else if("rsjOrgSame".equals(command)){  //单位日数剧同比                
                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                list=rsjService.findAllRsjSame(currentOrgCode,dateFrom,dateTo,range,reportStatus,0,99999);
            }else if("rsjLockOrgSame".equals(command)){  //单位日数剧同比                
                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                list=rsjService.findAllRsjLockSame(currentOrgCode,dateFrom,dateTo,range,reportStatus,0,99999);
            }else if("rsjReportOrg".equals(command)){
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String dateTo = request.getParameter("dateTo");       
                
                String dateFrom = dateTo.substring(0,6) + "01";
                //System.out.println("*"+dateTo+"*"+dateFrom);
                list=rsjService.findAllOrgRsjReport(currentOrgCode,dateFrom,dateTo,range,reportStatus,lock);
            }else if("dayException".equals(command)){
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                String cond  = TheUtil.getPointSelectCondition(request);
                list = rsjService.findAllRateException(currentOrgCode, dateFrom, dateTo, cond, start, limit, lock);
            }else if("rTimeCard".equals(command)){
                int start = Integer.parseInt(request.getParameter("start"));
                int limit = Integer.parseInt(request.getParameter("limit"));
                String yearTo = request.getParameter("yearTo");
                String monthTo = request.getParameter("monthTo");
                String cond  = TheUtil.getPointSelectCondition(request);
                list = rsjService.findAllTimeCard(currentOrgCode, yearTo, monthTo, cond, start, limit, lock);
            }
            
            
            String jsonText = null;
            jsonText = JsonUtil.JavaMapListToGridFormat("data", list, rsjService.getSize());
            out.printf(jsonText);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
